package com.atguigu.gmall.realtime.joindemo;

import com.atguigu.gmall.realtime.app.BaseSQLApp;
import com.atguigu.gmall.realtime.util.SQLUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2022/12/6 10:12
 */
public class JoinInnerDemo extends BaseSQLApp {
    public static void main(String[] args) {
        new JoinInnerDemo().init(
            8876,
            2,
            "JoinInnerDemo"
        );
    }
    
    @Override
    protected void handle(StreamExecutionEnvironment env,
                          StreamTableEnvironment tEnv) {
        tEnv.executeSql("create table t1(" +
                            " id int, " +
                            " name string " +
                            ")" + SQLUtil.getKafkaSourceDDL("t1", "atguigu", "csv"));
    
        tEnv.executeSql("create table t2(" +
                            " id int, " +
                            " age int " +
                            ")" + SQLUtil.getKafkaSourceDDL("t2", "atguigu", "csv"));
        
        // 内连接
        // 默认所有两表的所有数据都会永久的存储到内存中
        // 需要给数据加 ttl
        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(20));
//        tEnv.getConfig().getConfiguration().setString("table.exec.state.ttl", "10 s");
        tEnv.sqlQuery("select " +
                          " t1.id, " +
                          " name, " +
                          "  age " +
                          "from t1 " +
                          "join t2 on t1.id=t2.id")
            .execute()
            .print();
    }
}
